import axios from 'axios';
import express from 'express';
import fs from 'fs';
import path from 'path';
const app = express();

//设置跨域访问
app.all('*', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  // res.header("Access-Control-Allow-Origin", "http://192.168.137.1:8080");
  res.header("Access-Control-Allow-Headers", "X-Requested-With,sign,reqType,x-token"); // 允许前端设置的请求头
  res.header("Access-Control-Expose-Headers", "x-token"); // 允许前端读取返回自定义头部信息，默认只能获取Cache-Control、Content-Language、Content-Type、Expires、Last-Modified、Pragma
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By",' 3.2.1');
  res.header("Content-Type", "application/json;charset=utf-8");
  res.header("Access-Control-Allow-Credentials", true);
  next();
});




function get(url,params) {
  return axios.get(url, {...params})
}

//写个接口123
app.get('/123',function(req,res){
  res.setHeader('x-token', 'bar');
  res.status(200);
  setTimeout(function() {
    res.json({
      code: 200,
      data: {a:1}
    })
  },1000);
});

app.get('/api/getWeather', (req, res) => {
  console.log(res.query)
  const url = `https://api.map.baidu.com/weather/v1/?district_id=110100&data_type=all&ak=6GrqtvsS80ywrqyXV3YcuIMBcNPVvnGF`;
  get(url).then(response => {
    const data = response.data
    res.json(data)
  })
})


//配置服务端口

const server = app.listen(3100, 'localhost', function () {

  const host = server.address().address;

  const port = server.address().port;

  console.log('示例应用侦听 http://%s:%s', host, port);
})
